package com.gamesnet.ihm;

import java.util.List;

import org.json.JSONException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

import com.gamesnet.api.Api;
import com.gamesnet.api.Session;
import com.gamesnet.models.Listable;
import com.gamesnet.models.Publication;
import com.gamesnet.models.Utilisateur;
import com.gamesnet.util.AdapterListener;
import com.gamesnet.util.MessageDialog;
import com.gamesnet.widget.ActionBar;
import com.gamesnet.widget.ActionBar.IntentAction;
import com.gamesnet.widget.ListAdapter;

public class ActuActivity extends Activity implements AdapterListener{
	
	@Override
	public void onCreate(Bundle savedInstanceState) { 

		super.onCreate(savedInstanceState); 
		setContentView(R.layout.wall);

		final ActionBar actionBar = (ActionBar) findViewById(R.id.actionbarHaut);
		actionBar.setHomeAction(new IntentAction(this, new Intent(this, DashboardActivity.class), R.drawable.home));
		actionBar.setTitre(PageTitle.ACTU);

		Utilisateur currentUser = Session.getInstance().getUser();

		try {
			List<Publication>publications = Api.actu(String.valueOf(currentUser.getId()));
			ListAdapter<Publication> adapter = new ListAdapter<Publication>(this, publications);
			adapter.setUserId(currentUser.getId());
			adapter.addListener(this);
			ListView list = (ListView)findViewById(R.id.listPublication);
			list.setAdapter(adapter);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void onClickText(Listable model, int position) {
		Intent intent = new Intent(this, PublicationActivity.class);
		intent.putExtra("postId", model.getId());
		startActivity(intent);

	}

	public void onClickSupp(Listable model, int position) {
		MessageDialog.showMessage("supprimer"+model.afficher(), this);

	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) { 
		super.onCreateOptionsMenu(menu);
		menu.add(0, 1, 0, "Publiez");
		return true; 
	}

	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) { case 1:
			Intent intent = new Intent(this, NewPublicationActivity.class);
			intent.putExtra("userId", Session.getInstance().getUser().getId());
			startActivity(intent);
			return true;
		default:
			return true;
		} 
	}


}
